package com.hzhr.iot.device.service;

import com.hzhr.iot.core.annotation.ApiImplicitInternalParams;
import com.hzhr.iot.core.annotation.CygService;
import com.hzhr.iot.core.constant.AccessModeEnum;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrJsonUtil;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.constant.Device_ErrorsEnum;
import com.hzhr.iot.device.convertor.custom.CollectDeviceCustomConvertor;
import com.hzhr.iot.device.dao.CollectDeviceDao;
import com.hzhr.iot.device.dao.mapper.custom.PcCollectDeviceIotDeviceMapper;
import com.hzhr.iot.device.entity.CollectDeviceEntity;
import com.hzhr.iot.device.entity.ext.CollectDeviceEntityExt;
import com.hzhr.iot.device.entity.ext.IotDeviceEntityExt;
import com.hzhr.iot.device.model.collectDevice_collectException.CollectDevice_CollectExceptionGetModel;
import com.hzhr.iot.device.model.collectDevice_implRecord.CollectDevice_ImplRecordGetModel;
import com.hzhr.iot.device.model.collectDevice_implRecord.CollectDevice_ImplRecordModel;
import com.hzhr.iot.device.model.regulatorDevice_monitor.RegulatorDevice_MonitorGetModel;
import com.hzhr.iot.device.model.regulatorDevice_monitor.RegulatorDevice_MonitorModel;
import com.hzhr.iot.device.queryModel.custom.CollectDeviceCustomQueryModel;
import com.hzhr.iot.device.service.base.BaseService;
import com.hzhr.iot.statis.sdk.StatisSdk;
import com.hzhr.iot.statis.sdk.model.deviceDataException.DeviceDataExceptionGetModel;
import com.hzhr.iot.statis.sdk.service.Statis_DeviceDataExceptionService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
//@CygService(entity = IotDeviceEntity.class, model = ConcentratorModel.class)
public class CollectDevice_CollectExceptionService extends BaseService {

    @Autowired
    CollectDeviceCustomConvertor collectDeviceCustomConvertor;

    @Autowired
    CollectDeviceDao collectDeviceDao;

    @Autowired
    Statis_DeviceDataExceptionService statis_deviceDataExceptionService;





    //region javadoc注释 : get
    /**
     * 查询采集设备的采集异常 (×) <br/><br/>
     * 1. 查询采集设备，如果采集设备不存在抛出异常201； <br/>
     * 2. 查询采集设备的采集异常； <br/>
     * 3. 返回信息； <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _page : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 页数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _count : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 每页数量;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _show_total : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示总数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _equal_exceptionType : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : where statis-service.DeviceDataExceptionModel.exceptionType = #{_equal_exceptionType};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _greaterthanorequal_exceptionDayCount : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : where statis-service.DeviceDataExceptionModel.exceptionDayCount >= #{_greaterthanorequal_exceptionDayCount};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _greaterthanorequal_statisDate : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : where statis-service.DeviceDataExceptionModel.statisDate >= #{_greaterthanorequal_statisDate};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : long;<br/>
     * _lessthan_statisDate : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : where statis-service.DeviceDataExceptionModel.statisDate < #{_lessthan_statisDate};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : long;<br/><br/>
     * @param collectDeviceId 采集设备编号 <br/><br/>
     * @throws ServiceException <br/>
     * @see CollectDevice_CollectExceptionGetModel result <br/>
     */ 
    //endregion
    //region 说明 : get
    //查询采集设备的采集异常 (×)
    //1. 查询采集设备，如果采集设备不存在抛出异常201；
    //2. 查询采集设备的采集异常；
    //3. 返回信息；
    //endregion
    //region 参数 : get
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "collectDeviceId", value = "采集设备编号",required = true, dataType = "int", paramType = "path"),

            @ApiImplicitParam(name = "page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "equal_exceptionType", value = "where CollectDevice_CollectExceptionModel.exceptionType = #{equal_exceptionType}", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "greaterthanorequal_exceptionDayCount", value = "where CollectDevice_CollectExceptionModel.exceptionDayCount >= #{greaterthanorequal_exceptionDayCount}", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "greaterthanorequal_statisDate", value = "where CollectDevice_CollectExceptionModel.statisDate >= #{greaterthanorequal_statisDate}", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "lessthan_statisDate", value = "where CollectDevice_CollectExceptionModel.statisDate < #{lessthan_statisDate}", required = false, dataType = "int", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_equal_exceptionType", value = "where statis-service.DeviceDataExceptionModel.exceptionType = #{_equal_exceptionType}", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_greaterthanorequal_exceptionDayCount", value = "where statis-service.DeviceDataExceptionModel.exceptionDayCount >= #{_greaterthanorequal_exceptionDayCount}", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_greaterthanorequal_statisDate", value = "where statis-service.DeviceDataExceptionModel.statisDate >= #{_greaterthanorequal_statisDate}", required = false, dataType = "long", paramType = "query"),
            @ApiImplicitParam(name = "_lessthan_statisDate", value = "where statis-service.DeviceDataExceptionModel.statisDate < #{_lessthan_statisDate}", required = false, dataType = "long", paramType = "query"),
    })
    //endregion
    //region 请求 : get
    @RequestMapping(value = "/collect-devices/{collectDeviceId}/collect-exceptions", method = RequestMethod.GET)
    //endregion
    public CollectDevice_CollectExceptionGetModel get(Map<String, String> header, Integer collectDeviceId, Map<String, Object> query) throws ServiceException {

        //查询采集设备的采集异常
        //1. 查询采集设备，如果采集设备不存在抛出异常201；
        //查询CollectDeviceEntity对象collectDevice_1，查询规则如下：
        //(1) CollectDeviceEntity.id =  路径参数collectDeviceId；
        //(2) CollectDeviceEntity.isValid = '1'；
        //如果collectDevice_1不存在，抛出异常201；
        CollectDeviceCustomQueryModel queryModel = new CollectDeviceCustomQueryModel();
        queryModel.setShow(true);
        queryModel.setEqual_id(collectDeviceId);
        queryModel.setEqual_isValid("1");
        CollectDeviceEntityExt collectDevice_1 = collectDeviceDao.selectExt(queryModel);
        if (collectDevice_1 == null){
            throw new ServiceException(Device_ErrorsEnum.COLLECTDEVICE_CODE_SERVICE_PUT_201);
        }

        String _show = HzhrTypeUtil.object2string(query.get("_show"));
        //2. 查询采集设备的采集异常；
        //查询DeviceDataExceptionGetModel对象集合dataExceptions_1及总数，查询规则如下：
        //(1) 根据条件参数查询；
        //(2) DeviceDataExceptionModel.collectDeviceId = 路径参数collectDeviceId；
        DeviceDataExceptionGetModel dataExceptions_1 = null;
        query.put("_equal_collectDeviceId",collectDeviceId);
        if (_show != null) {
             dataExceptions_1 = statis_deviceDataExceptionService.get(initHeader(header, AccessModeEnum.GLOBAL),query);
        }

        //        //3. 返回信息；
        //        //根据条件参数、dataExceptions_1及总数生成CollectDevice_CollectExceptionGetModel对象result并返回;
        CollectDevice_CollectExceptionGetModel result = HzhrJsonUtil.convertObjectType(dataExceptions_1,CollectDevice_CollectExceptionGetModel.class);
        return result;
    }
}

